local CPloot = LibStub("AceAddon-3.0"):GetAddon("CPloot")

local CPlootPoints = {}

-- Constants (to be parsed as argument to AwardPoints and to be used when calculating Mount loot points on StartNewTier and AddTeamMember):
-- OnTimeBonus = 1
-- BossKill = 1
-- OneShot = 2
-- ProgressKill = 5
-- RemainingBossWeight = 50

-- NOTICE: The below is based on early thoughts and will need changes to fit into the addon

function CPlootPoints:OnInitialize()
	-- Initialize points module here
	CPloot:Print("CPlootPoints:OnInitialize() called")
end

function CPlootPoints:OnEnable()
	-- OnEnable actions for module goes here
end

function CPlootPoints:IsValidPointString(pointString)
	if ( string.match((string.sub(pointString,1,22)),("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d")) ) then
		-- part 1 matches format
		if ( string.match((string.sub(pointString,23,23)),(";")) ) then
			-- part 2 also matches format
			if ( string.match((string.sub(pointString,24,31)),("%d%d%d%d%d%d%d%d")) ) then
				-- part 3 also matches format => return true
	  		return true
	  	end
	  end
	end
	-- format did not match => return false
	return false
end

function CPlootPoints:GetPointsFromString(pointString)
	if ( CPlootPoints:IsValidPointString(pointString)) then
		-- valid points string found, return existing points in table
		return {
			["Part1WeaponMS"] = tonumber(string.sub(pointString,1,3)),
			["Part2WeaponOS"] = tonumber(string.sub(pointString,4,6)),
			["Part3ArmorMS"] = tonumber(string.sub(pointString,7,9)),
			["Part4ArmorOS"] = tonumber(string.sub(pointString,10,12)),
			["Part5TierMS"] = tonumber(string.sub(pointString,13,15)),
			["Part6TierOS"] = tonumber(string.sub(pointString,16,18)),
			["Part7Mount"] = tonumber(string.sub(pointString,19,22)),
			["Part8LastPayed"] = tonumber(string.sub(pointString,24,27)),
			["Part9NextToLastPayed"] = tonumber(string.sub(pointString,28,31)),
			["ValidString"] = true,
		}

	else
		-- invalid points string found, return new points table with 0 points
		return {
			["Part1WeaponMS"] = 0,
			["Part2WeaponOS"] = 0,
			["Part3ArmorMS"] = 0,
			["Part4ArmorOS"] = 0,
			["Part5TierMS"] = 0,
			["Part6TierOS"] = 0,
			["Part7Mount"] = 0,
			["Part8LastPayed"] = 0,
			["Part9NextToLastPayed"] = 0,
			["ValidString"] = false,
		}
	end
end

function CPlootPoints:CreatePointString(Part1WeaponMS,Part2WeaponOS,Part3ArmorMS,Part4ArmorOS,Part5TierMS,Part6TierOS,Part7Mount,Part8LastPayed,Part9NextToLastPayed)
	return string.format("%03d",tostring(Part1WeaponMS)) ..
	string.format("%03d",tostring(Part2WeaponOS)) ..
	string.format("%03d",tostring(Part3ArmorMS)) ..
	string.format("%03d",tostring(Part4ArmorOS)) ..
	string.format("%03d",tostring(Part5TierMS)) ..
	string.format("%03d",tostring(Part6TierOS)) ..
	string.format("%04d",tostring(Part7Mount)) ..
	";" ..
	string.format("%04d",tostring(Part8LastPayed)) ..
	string.format("%04d",tostring(Part9NextToLastPayed))
end

function CPlootPoints:GetRaidPoints()
	local PointsTable = {}
	
	for i=1, GetNumGroupMembers() do
		local name, _, _, _, _, _, _,
		_, _, _, _ = GetRaidRosterInfo(i)
		
		print ("name: " .. name)
		
		for x=1,  GetNumGuildMembers() do
			local fullName, _, _, _, _, _, _, officernote, _, _, _, _,
			_, _, _, _ = GetGuildRosterInfo(x)
			
			realName = CPloot:GetRealPlayerName(fullName)
			
			if (name == realName) then
				print ("match found: " .. realName .. " at index: " .. x .. " with officernote: " .. officernote)
				PointsTable[realName] = CPlootPoints:GetPointsFromString(officernote)
			end
		end
	    
	end

	return PointsTable

end

function CPlootPoints:GetPointsCategory(Category,Spec)
	if (Category == "Weapon") then
		if (Spec == "MainSpec") then
			return "Part1WeaponMS"
		else
			return "Part2WeaponOS"
		end
	end
	if (Category == "Armor") then
		if (Spec == "MainSpec") then
			return "Part3ArmorMS"
		else
			return "Part4ArmorOS"
		end
	end
	if (string.subt(Category,1,4) == "Tier") then
		if (Spec == "MainSpec") then
			return "Part5TierMS"
		else
			return "Part6TierOS"
		end
	end
	if (Category == "Mount") then
		return "Part7Mount"
	end			
	
	return nil
	
end


function CPlootPoints:AwardRaidPoints(points)
-- Awards loot points to all lists for all players in raid. Takes number of points to award as argument

--[[
local OnTimeBonus = 1

for i=1, GetNumGroupMembers() do
	local name, _, _, _, _, _, _,
	_, _, _, _ = GetRaidRosterInfo(i)
	
	print ("name: " .. name)
	
	for x=1,  GetNumGuildMembers() do
		local fullName, _, _, _, _, _, _, officernote, _, _, _, _,
		_, _, _, _ = GetGuildRosterInfo(x)
		
		if (name == Floot:GetRealPlayerName(fullName)) then
			print ("match found: " .. fullName .. "at index: " .. x)
			if (officernote == nil) then
				GuildRosterSetOfficerNote(x, points)
			else
				GuildRosterSetOfficerNote(x, officernote + points)
			end
		end
	end
    
end
]]

end

function CPlootPoints:AwardPlayerPoints(name,points)
-- Awards loot points to named player. Takes number of points to award as 2nd argument
end

function CPlootPoints:AwardLoot(name,category,itemname,movedmodifier)
-- Awards loot to player and sets <category>Pay = <category> followed by setting <category> = (0 + <movedmodifier>) and announces handout and price payed.
-- <movedmodifier> should be 0 if it's not movement of an item.
end

function CPlootPoints:RefundLoot(name,category)
-- Sets <category> = (<category> + <category>Pay) => Returns <category> value from before update to be used as compensate value to player item is moved to
end

function CPlootPoints:MoveLoot(fromname,toname,categoryfrom,categoryto,itemname)
-- Calls RefundLoot for <fromplayer> then AwardLoot for <toplayer> with returned compensate value as <movedmodifier>
end

function CPlootPoints:DeductRaidPoints(points)
-- Deducts loot points from all lists for all players in raid. Takes number of points to deduct as argument. This is only used to correct errors if wrong points was awarded.
end

function CPlootPoints:DeductPlayerPoints(name,points)
-- Deducts loot points from named player. Takes number of points to deduct as 2nd argument. This is only to be used if needing to deduct points from a player manually and is not used for resetting on wins
end

function CPlootPoints:StartNewTier(bossesremaining)
-- Resets all lists for all players in guild and sets Mount loot points to <bossesremaining> x RemainingBossWeight for players with raider rank - Confirmation box required in UI calling this!
end

-----------------------------------
-- Used to get the points class ---
-----------------------------------
function CPloot:NewCPlootPoints()
   return CPlootPoints
end